package com.ra.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ra.dao.LoginDAO;
import com.ra.model.product.Product;
import com.ra.model.user.UserProfile;
import com.ra.util.SendMail;

public class LoginController {

	private static Logger log = Logger.getLogger(LoginController.class);
	private LoginDAO loginDAO = null;

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/userLogin.html")
	public ModelAndView userLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug(" userLogin :: start ");
		UserProfile userProfile = new UserProfile();
		ModelAndView modelAndView = new ModelAndView("user/userLogin");
		modelAndView.addObject("userProfile", userProfile);
		return modelAndView;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/userRegistration.html")
	public ModelAndView userRegistration(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug(" userRegistration :: start ");
		UserProfile userProfile = new UserProfile();
		ModelAndView modelAndView = new ModelAndView("user/userRegistration");
		modelAndView.addObject("userProfile", userProfile);
		return modelAndView;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/doLogin.html")
	public ModelAndView doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug(" doLogin :: start ");
		ModelAndView modelAndView = null;
		List<Product> products = new ArrayList<Product>();
		try {
			log.debug("doLogin :: logic ");
			UserProfile userProfile = getLoginDAO().getUser(
					request.getParameter("userId"), request.getParameter("pw"));

			boolean loginStatus = false;
			if (null != userProfile) {
				loginStatus = userProfile.isActive();
			}
			
			log.debug("doLogin :: loginStatus " + loginStatus);

			if (loginStatus) {
				modelAndView = new ModelAndView("listProducts");
				modelAndView.addObject("listProducts", products);
			} else {
				modelAndView = new ModelAndView("user/userLogin");
				modelAndView.addObject("userProfile", new UserProfile());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/doRegister.html")
	public ModelAndView doRegister(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug(" doRegister :: start ");
		UserProfile userProfile = new UserProfile();
		userProfile.setUserId(request.getParameter("userId"));
		userProfile.setFirstName(request.getParameter("firstName"));
		userProfile.setLastName(request.getParameter("lastName"));
		userProfile.setEmail(request.getParameter("email"));
		userProfile.setPassword(request.getParameter("password"));
		ModelAndView modelAndView = new ModelAndView("/index");
		try {
			log.debug("doRegister :: logic ");
			getLoginDAO().registerUser(userProfile);
			log.debug("doRegister :: regStatus ");
			SendMail s = new SendMail();
			String emailSubjectTxt = "Welcome to RA!! ";
			String[] sendTo = { request.getParameter("email") };
			String emailMsgTxt = "Please click the link below to confirm your registration !! \n\n http://localhost:8080/ra.war/verification/"+userProfile.getUserId()+"|"+userProfile.getVerificationCode();
			new SendMail().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt,
					"revdeals@gmail.com");
		} catch (Exception e) {
			e.printStackTrace();
		}
		// modelAndView.addObject("userProfile", userProfile);
		return modelAndView;
	}

	public void setLoginDAO(LoginDAO loginDAO) {
		this.loginDAO = loginDAO;
	}

	public LoginDAO getLoginDAO() {
		return loginDAO;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/verification.html")
	public ModelAndView doVerification(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug(" doVerification :: start ");
		ModelAndView modelAndView = null;
		try {
			log.debug("doVerification :: logic ");
			//TODO: logic to fetch user
			String verificationCode = request.getParameter("verificationCode");
			
			String[] temp;
			temp = verificationCode.split("\\|");
			
			UserProfile userProfile = getLoginDAO().getUser(temp[0]);
			
			//UserProfile userProfile = getLoginDAO().getUser("sureshrk19", "suresh123");
			if (null != userProfile) {
				if (temp[1].equals(userProfile.getVerificationCode())) {
					userProfile.setActive(true);
					getLoginDAO().updateUser(userProfile);
				}
			}
			log.debug("doVerification :: loginStatus " + userProfile.isActive());

			modelAndView = new ModelAndView("user/userLogin");
			modelAndView.addObject("userProfile", new UserProfile());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

}
